package indi.kider.server.woniubus.api;


import indi.kider.common.model.apabus.Reservation;
import indi.kider.common.model.apabus.ReservationQuery;
import indi.kider.server.woniubus.service.ReservationService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "reservation")
@Log
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    /**
     * @api {POST} /pageList
     * @apiGroup apartbus
     * @apiDescription 根据字段查询获取预约列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/reservation/pageList
     * {"num":"0","size":"8","query":{apartmentId:0,apartmentName:"",reservationName:"",phone:"",eventTime:null,appointmentTime:null,remark:"",id:0,state:0}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{apartmentId:0,apartmentName:"",reservationName:"",phone:"",eventTime:null,appointmentTime:null,remark:"",id:0,state:0}],"total":1,"size":0}}
     */
    @PostMapping("/pageList")
    public CommonResponse<List<Map>> getReservationList(@RequestBody PageRequest<ReservationQuery> request) {
        return CommonResponse.buildGetSuccessResponse(reservationService.getReservationPageList(request));
    }

    /**
     * @api {PUT} /add
     * @apiGroup apartbus
     * @apiDescription 预约
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/reservation/add
     * {apartmentId:0,reservationName:"",phone:"",eventTime:null,appointmentTime:null,info1:"",state:0}
     * @apiSuccessExample {json} 返回样例:
     * {"status": "202","msg": "保存成功！","data": ""}
     */
    @PutMapping("/add")
    public CommonResponse addReservation(@RequestBody Reservation reservation) {
        try {
            reservationService.reservation(reservation);
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {PUT} /update
     * @apiGroup apartbus
     * @apiDescription 更新预约
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/reservation/update
     * {id:0,state:0,info2:''}
     * @apiSuccessExample {json} 返回样例:
     * {"status": "202","msg": "保存成功！","data": ""}
     */
    @PutMapping("/update")
    public CommonResponse updateReservation(@RequestBody Reservation reservation) {
        try {
            reservationService.updateReservationState(reservation);
            return CommonResponse.buildUpdateSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
